package com.lyn.module_custom_utils;


import android.content.SharedPreferences;

import com.tencent.mmkv.MMKV;

/**
 * @author:longyongning
 * @date:2021/10/2
 * @description: MMKV储存工具栏
 */

public class SpUtil {

    private static final String FILE_NAME = "setting";
    private MMKV mmkv;

    private static class PreferenceUtilClassInstance {
        private static final SpUtil instance = new SpUtil();
    }

    public static SpUtil getInstance() {
        return PreferenceUtilClassInstance.instance;
    }

    public SpUtil() {
        mmkv = MMKV.mmkvWithID(FILE_NAME);
    }


    public void clear() {
        mmkv.edit().clear().apply();
    }

    public void setString(String key, String value) {
        try {
            SharedPreferences.Editor editor = mmkv.edit();
            editor.putString(key, value);
            editor.commit();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void setInt(String key, int value) {
        try {
            SharedPreferences.Editor editor = mmkv.edit();
            editor.putInt(key, value);
            editor.commit();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void setFloat(String key, float value) {
        try {
            SharedPreferences.Editor editor = mmkv.edit();
            editor.putFloat(key, value);
            editor.commit();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void setLong(String key, long value) {
        try {
            SharedPreferences.Editor editor = mmkv.edit();
            editor.putLong(key, value);
            editor.commit();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void setBoolean(String key, boolean value) {
        try {
            SharedPreferences.Editor editor = mmkv.edit();
            editor.putBoolean(key, value);
            editor.commit();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public String getString(String key, String value) {
        return mmkv.getString(key, value);
    }

    public boolean getBoolean(String key, boolean value) {
        return mmkv.getBoolean(key, value);
    }

    public int getInt(String key, int value) {
        return mmkv.getInt(key, value);
    }

    public long getLong(String key, Long value) {
        return mmkv.getLong(key, value);
    }

    public float getFloat(String key, float value) {
        return mmkv.getFloat(key, value);
    }

    public byte[] getByte(String key, byte[] value) {
        return mmkv.getBytes(key, value);
    }


}
